<template>
  <div class="ad-card-wrapper" @click="clickAds(data)">
    <el-image class="ad-image" :src="data.thumbnail || data.img" :alt="data.name" lazy
      :preview-src-list="[data.thumbnail || data.img]" />
    <div class="text-box">
      <div class="text-box__title">
        {{ data.name }}
      </div>
      <div class="text-box__description">
        {{ data.note }}
      </div>
    </div>
    <div class="ads-control">
      <el-tooltip effect="dark" :content="showAds ? '隐藏广告' : '显示广告'" placement="left">
        <el-button type="text" @click.stop="toggleAds()" class="ads-toggle-btn" :aria-label="showAds ? '隐藏广告' : '显示广告'"
          :aria-pressed="showAds">
          <transition name="fade" mode="out-in">
            <svg-icon :key="showAds ? 'open' : 'closed'" :icon-class="showAds ? 'eye_open_1' : 'eye_close_1'" />
          </transition>
        </el-button>
      </el-tooltip>
    </div>
  </div>
</template>

<script>
import {
  defineComponent
} from 'vue';
import mutiLine from '@/mixins/text-line';

export default defineComponent({
  name: 'AdCard',
  mixins: [mutiLine],
  props: {
    data: {
      type: Object,
      required: true
    },
    isShow: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      showAds: this.isShow
    };
  },
  methods: {
    clickAds(data) {
      if (data.url) {
        window.open(data.url, '_blank');
      }
    },
    toggleAds() {
      //修改父组件属性 isShow属性
      this.$emit('toggleAds');
    }
  }
});
</script>

<style lang="scss" scoped>
@import '@/styles/mixin.scss';

.ad-card-wrapper {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  position: relative;
  height: 100%;
  width: 100%;
  border-radius: 5px;
  cursor: pointer;
}

.ads-control {
  position: absolute;
  top: -20px;
  right: 0px;
}

.ads-toggle-btn {
  color: var(--el-text-color-secondary);
  transition: all 0.2s ease;
  width: 16px;
  height: 16px;

  &:hover {
    color: var(--primary-500);
    transform: scale(1.2);
  }

  .svg-icon {
    width: 20px;
    height: 20px;
    transition: all 0.2s ease;
  }
}

.ad-card {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  height: 100%;
  width: 100%;
  border-radius: 5px;
  cursor: pointer;
}

.ad-image {
  flex: 0 0 30%;
  max-width: 30%;
  height: 100%;
  border-radius: 12px;
  transition: transform 0.3s ease;
  position: relative;
  padding-right: 10px;

  &:hover {
    height: 100%;
    z-index: 2;
    transform: scale(1.05);
  }
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  max-width: 100%;
  height: 100%;
  font-size: 14px;
}

.text-box {
  @include flex-column;
  text-align: center;
  max-width: 70%;
  gap: 8px;
}

.text-box__title {
  display: block;
  font-size: var(--text-xl);
  font-weight: bold;
  color: var(--primary-600);
  width: 100%;
  min-height: 30px;
  white-space: normal;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: all 0.2s ease;
  position: relative;

  @media (max-width: 768px) {
    font-size: var(--text-lg);
    max-width: 200px;
  }
}

.text-box__description {
  display: flex;
  flex-direction: column;
  text-align: start;
  text-indent: 1em;
  gap: 12px;
  letter-spacing: 1px;
  line-height: 1.6;
}
</style>
